package com.cskaoyan.controller.wx.service;

import com.cskaoyan.bean.entity.Collect;
import com.cskaoyan.bean.entity.CollectExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.CollectInfoVo;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <h1> 小程序端 我的收藏 </h1>
 * Created by GaoJia
 * Date  2021/10/23 19:50
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Resource
    CollectMapper collectMapper;

    @Override
    public boolean addOrDelete(Map map) {
        Integer valueId = (Integer) map.get("valueId");

        Collect collect = new Collect();
        collect.setValueId(valueId);
        collect.setUpdateTime(new Date());

        // select count(`id`) from market_collect where `valueId`= #{valueId}
        // =1 删除  =0 插入
        // TODO这里使用逻辑删除还没能继续优化
        if (1 == collectMapper.existById(valueId)){
            collectMapper.deleteByValueId(valueId);
        } else {
            collectMapper.insertSelective(collect);
        }
        return true;
    }

    @Override
    public BasePageData query(CollectInfoVo collectInfoVo) {
        CollectExample collectExample = new CollectExample();
        List<CollectInfoVo> collects = collectMapper.selectByExample2(collectExample);
        return BasePageData.ok(collects);
    }

    /**
     *  获取最新的商品信息
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getNewestGoodsByUserId(Integer userId) {
        Integer[] goodsIds = collectMapper.selectGoodsIdByUserId(userId);
        return goodsIds[goodsIds.length - 1];
    }
}
